package de.baumann.browser.activitys.node

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.Menu
import android.view.MenuItem
import android.view.View
import de.baumann.browser.OdinGlideModule
import de.baumann.browser.R
import de.baumann.browser.adapter.NodeProgressAdapter
import de.baumann.browser.api.net.vo.NodeProgress
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.iview.INodeStatusView
import de.baumann.browser.present.NodeStatusPresenter
import de.baumann.browser.utils.getUserId
import kotlinx.android.synthetic.main.activity_node_status.*
import kotlinx.android.synthetic.main.include_tool_bar.*

/**
 *@author:
 *@date:2019/9/25 16:24
 *@discription: 节点状态 (节点创建状态 || 用户加入节点的状态)
 */
@Deprecated("5.0.0版本之后活动结束，不再适用")
class NodeStatusActivity : BaseOdinActivity<INodeStatusView, NodeStatusPresenter>(), INodeStatusView {

    override fun createPresenter(): NodeStatusPresenter {
        return NodeStatusPresenter()
    }

    override fun createView(): INodeStatusView {
        return this
    }

    companion object {
        fun startNodeStatusActivity(context: Context, nodeId: String) {
            val intnet = Intent(context, NodeStatusActivity::class.java).apply {
                putExtra("nodeId", nodeId)
            }
            context.startActivity(intnet)
        }
    }

    private lateinit var progressAdapter: NodeProgressAdapter
    private var ownerId = ""
    private var nodeName = ""
    private var nodeDesc = ""
    private var nodeId = ""

    override fun getLayout(): Int {
        return R.layout.activity_node_status
    }

    override fun initView() {
        initToolbar(commToolBar,null)
        rvNodeProgress.layoutManager = LinearLayoutManager(mContext)
        progressAdapter = NodeProgressAdapter(R.layout.item_node_progress)
        rvNodeProgress.adapter = progressAdapter
        tvStatusNodeMembers.setOnClickListener {
            NodeMemberActivity.startNodeMemberActivity(mContext, nodeId)
        }

        cardStatus.setOnClickListener {
            NodeDetailActivity.startNodeDetailActivity(mContext, nodeId)
        }

        ivBtnUpdateDesc.setOnClickListener {
            CreateNodeActivity.startCreateNodeActivityForUpdate(this@NodeStatusActivity,
                    nodeId,
                    nodeName,
                    nodeDesc,
                    2031)
        }
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        print("onNewIntentonNewIntentonNewIntentonNewIntentonNewIntent")
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_node_status, menu)
        return super.onCreateOptionsMenu(menu)
    }


    override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
        menu!!.findItem(R.id.menu_joins).isVisible = ownerId == getUserId()
        if (ownerId == getUserId()) {
            ivBtnUpdateDesc.visibility = View.VISIBLE
        }else{
            ivBtnUpdateDesc.visibility = View.GONE
        }
        return super.onPrepareOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when (item!!.itemId) {
            R.id.menu_joins -> RequestJoinActivity.startRequestJoinActivity(mContext, nodeId)
            R.id.menu_members -> NodeListActivity.startNodeListActivityForDisplay(this, 2030)
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data != null) {
            nodeId = data.getStringExtra("nodeId")
        }
        if (resultCode == Activity.RESULT_OK) {
            presenter.nodeStatus(nodeId)
            return
        }
    }


    override fun initData() {
        nodeId = intent.getStringExtra("nodeId")
        presenter.nodeStatus(nodeId)
    }

    @SuppressLint("SetTextI18n")
    override fun setNodeName(name: String) {
        nodeName = name
        commTitle.text = "${name}超级节点"
        tvStatusNodeName.text = "${name}超级节点"
    }

    override fun setNodeMembers(members: String) {
        tvStatusNodeMembers.text = members
    }

    override fun setNodeOwner(name: String) {
        tvOwner.text = name
    }

    override fun setOwnerId(ownerId: String) {
        this.ownerId = ownerId
        invalidateOptionsMenu()
    }

    override fun setCreateTime(time: String) {
        tvStatusNodeCreateTime.text = time
    }

    override fun setTotalHash(hash: String) {
        tvStatusHash.text = hash
        statusProgressNode.progress = hash.toDouble().toInt()
    }

    override fun setNodeProgress(progress: List<NodeProgress>) {
        progressAdapter.setNewData(progress)
    }

    override fun setNodeDesc(desc: String) {
        nodeDesc = desc
        tvNodeDesc.text = desc
    }

    override fun setHead(url: String) {
        OdinGlideModule.loadWithCropAndCircle(mContext,url,ivItemHostHead,R.drawable.icon_head)
    }

}
